//
//  CTHomePageLogic.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/21.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTHomePageLogic.h"
#import "CTDebugManager.h"
#import "CTRequestUtil.h"
#import "CTRequestErrorCode.h"
#import "CTUrl.h"
#import "CTFile.h"
#import "CTRequestFlip.h"
#import "CTHeader.h"
#import "CTAppInfo.h"


@implementation CTHomePageLogic


- (void)dealloc
{
    [CTDebugManager showDebugLogInfo:@"dealloc : CTHomePageLogic Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
       
    }
    return self;
}


-(void)hotSpotsRequest:(CTRequestHotSpotsItem *)item
successCompletionHandler:(requestSuccessBlock)successBlock
         failedHandler:(requestFailedBlock)failedBlock
{
        NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
        @try {
            [param setObject:item.token forKey:@"token"];
            [param setObject:item.timelimit forKey:@"timelimit"];
            [param setObject:item.lang forKey:@"lang"];
            [param setObject:item.count forKey:@"count"];
            [param setObject:item.page forKey:@"page"];
            [param setObject:item.visitor forKey:@"visitor"];
        }
        @catch (NSException *exception) {
            [CTDebugManager showErrorLogInfo:@"组装首页热点数据错误！"];
        }
    
        [CTRequestUtil sendAsyGetRequest:CT_URL_HOMEPAGE_HOTSPOTS(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
            BOOL isParserSuccess = [self parserHotSpotData:data];
            if (isParserSuccess == YES) {
                successBlock();
            }
            else
            {
                failedBlock();
            }
    
        } withFailedHandler:^(NSError *error) {
            failedBlock();
        }];
}


-(void)hotSpotsDetailRequest:(CTRequestHotSpotsDetailItem *)item
    successCompletionHandler:(requestSuccessBlock)successBlock
               failedHandler:(requestFailedBlock)failedBlock
{
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
    @try {
        [param setObject:item.token forKey:@"token"];
        [param setObject:item.timelimit forKey:@"timelimit"];
        [param setObject:item.lang forKey:@"lang"];
        [param setObject:item.count forKey:@"count"];
        [param setObject:item.page forKey:@"page"];
        [param setObject:item.visitor forKey:@"visitor"];
        [param setObject:item.group_id forKey:@"group_id"];
        [param setObject:item.like_num forKey:@"like_num"];
        [param setObject:item.comment_num forKey:@"comment_num"];
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"组装首页热点详情列表数据错误！"];
    }
    
    [CTRequestUtil sendAsyGetRequest:CT_URL_HOMEPAGE_HOTSPOTS_DETAIL(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
        BOOL isParserSuccess = [self parserHotSpotDetailsData:data withItm:item];
        if (isParserSuccess == YES) {
            successBlock();
        }
        else
        {
            failedBlock();
        }
        
    } withFailedHandler:^(NSError *error) {
        failedBlock();
    }];
}

//翻页Flip
-(void)hotSpotsDetailFlipRequest:(CTRequestHotSpotsDetailItem *)item
        successCompletionHandler:(requestSuccessBlock)successBlock
                   failedHandler:(requestFailedBlock)failedBlock
{
        BOOL isFlip = [CTRequestFlip isFlip:[CTFile getDicFile:@"hotspotDetailList.plist" fromDoc:CTFileDocLibrary fromPath:@"cacheData/homepage"]];
        if (isFlip == YES) {
    
            NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
            @try {
                [param setObject:item.token forKey:@"token"];
                [param setObject:item.timelimit forKey:@"timelimit"];
                [param setObject:item.lang forKey:@"lang"];
                [param setObject:item.count forKey:@"count"];
                [param setObject:item.visitor forKey:@"visitor"];
                [param setObject:item.group_id forKey:@"group_id"];
                [param setObject:item.like_num forKey:@"like_num"];
                [param setObject:item.comment_num forKey:@"comment_num"];
                [param setObject:item.page forKey:@"page"];
            }
            @catch (NSException *exception) {
                [CTDebugManager showErrorLogInfo:@"组装首页热点详情列表数据错误！"];
            }
    
            [CTRequestUtil sendAsyGetRequest:CT_URL_HOMEPAGE_HOTSPOTS_DETAIL(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
                BOOL isParserSuccess = [self parserHotSpotDetailsFlipData:data withItm:item];
                if (isParserSuccess == YES) {
                    successBlock();
                }
                else
                {
                    failedBlock();
                }
    
            } withFailedHandler:^(NSError *error) {
                failedBlock();
            }];
        }else
        {
            failedBlock();
        }
}


-(void)subjectRequest:(CTRequestSubjectItem *)item
successCompletionHandler:(requestSuccessBlock)successBlock
        failedHandler:(requestFailedBlock)failedBlock
{
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
    @try {
        [param setObject:item.token forKey:@"token"];
        [param setObject:item.lang forKey:@"lang"];
        [param setObject:item.group_id forKey:@"group_id"];
        [param setObject:item.visitor forKey:@"visitor"];
        
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"组装首页主题数据错误！"];
    }
    
    [CTRequestUtil sendAsyGetRequest:CT_URL_HOMEPAGE_SUBJECTS(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
        BOOL isParserSuccess = [self parserSubjectData:data withItm:item];
        if (isParserSuccess == YES) {
            successBlock();
        }
        else
        {
            failedBlock();
        }
        
    } withFailedHandler:^(NSError *error) {
        failedBlock();
    }];
}




#pragma mark - data parser -

-(BOOL)parserHotSpotData:(NSData *)resultData
{
   
    
    BOOL isParserSuccess = NO;
    
    @try {
        
        NSError * error;
        
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:resultData
                         options:NSJSONReadingAllowFragments
                         error:&error];
        if (jsonObject != nil && error == nil){
            if ([jsonObject isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
                
                NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                if ([errCode isEqualToString:@"0"]) {
                    
                    isParserSuccess = YES;
                    
                    NSDictionary * infoDic = [deserializedDictionary objectForKey:@"info"];
                    
                    //存入文件
                    
                    [CTFile cacheDicFile:[infoDic mutableCopy] fileName:@"hotspotList.plist" saveDoc:CTFileDocLibrary savePath:@"cacheData/homepage"];
                    
                }
                else{
                    
                    [CTFile removeFile:[NSString stringWithFormat:@"%@_visitor_token.text",[CTAppInfo appVersion]] fromDoc:CTFileDocDocuments fromPath:@"user"];
                    [CTFile removeFile:@"token.text" fromDoc:CTFileDocDocuments fromPath:@"user"];
                    //游客不存在
                    [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_VISITOR_ERROR_REQUEST object:nil];
                    
                    
                    NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                    [CTDebugManager showErrorLogInfo:errDescription];
                }
            }
        }
        else
        {
            [CTDebugManager showErrorLogInfo:@"解析首页热点数据错误！"];
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"解析首页热点数据错误！"];
    }
    
    return isParserSuccess;
}

-(BOOL)parserHotSpotDetailsData:(NSData *)resultData withItm:(CTRequestHotSpotsDetailItem *)itm
{
    
    
    BOOL isParserSuccess = NO;
    
    @try {
        
        NSError * error;
        
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:resultData
                         options:NSJSONReadingAllowFragments
                         error:&error];
        if (jsonObject != nil && error == nil){
            if ([jsonObject isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;

                NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                if ([errCode isEqualToString:@"0"]) {
                    
                    isParserSuccess = YES;
                    NSDictionary * infoDic = [deserializedDictionary objectForKey:@"info"];
                    
                    //存入文件
                    NSString * fileNameStr = [NSString stringWithFormat:@"%@.plist",itm.group_id];
                    [CTFile cacheDicFile:[infoDic mutableCopy] fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/homepage/hotspotList"];
                    
                }
                else{
                    NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                    [CTDebugManager showErrorLogInfo:errDescription];
                }
            }
        }
        else
        {
            [CTDebugManager showErrorLogInfo:@"解析热点详情列表数据错误！"];
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"解析热点详情列表数据错误！"];
    }
    
    return isParserSuccess;
}


-(BOOL)parserHotSpotDetailsFlipData:(NSData *)resultData withItm:(CTRequestHotSpotsDetailItem *)itm
{
        BOOL isParserSuccess = NO;
    
        @try {
    
            NSError * error;
    
            id jsonObject = [NSJSONSerialization
                             JSONObjectWithData:resultData
                             options:NSJSONReadingAllowFragments
                             error:&error];
            if (jsonObject != nil && error == nil){
    
                if ([jsonObject isKindOfClass:[NSDictionary class]]){
    
                    NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
                    NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                    if ([errCode isEqualToString:@"0"]) {
                        isParserSuccess = YES;
    
                        NSDictionary * infoDic = [deserializedDictionary objectForKey:@"info"];
                        //存入文件
                        NSString * fileNameStr = [NSString stringWithFormat:@"%@.plist",itm.group_id];
                        [CTFile cacheDicFile:[infoDic mutableCopy] fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/homepage/hotspotList"];
                        
                    }
                    else{
                        NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                        [CTDebugManager showErrorLogInfo:errDescription];
                    }
                }
            }
            else
            {
                [CTDebugManager showErrorLogInfo:@"解析热点详情列表数据错误！"];
            }
        }
        @catch (NSException *exception) {
            [CTDebugManager showErrorLogInfo:@"解析热点详情列表数据错误！"];
        }
        
        return isParserSuccess;
}

-(BOOL)parserSubjectData:(NSData *)resultData withItm:(CTRequestSubjectItem *)itm
{
    
    
    BOOL isParserSuccess = NO;
    
    @try {
        
        NSError * error;
        
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:resultData
                         options:NSJSONReadingAllowFragments
                         error:&error];
        if (jsonObject != nil && error == nil){
            if ([jsonObject isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
                
                NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                if ([errCode isEqualToString:@"0"]) {
                    
                    isParserSuccess = YES;
                    
                    NSDictionary * infoDic = [deserializedDictionary objectForKey:@"info"];
                
                    //存入文件
                    NSString * fileNameStr = [NSString stringWithFormat:@"%@.plist",itm.group_id];
                    [CTFile cacheDicFile:[infoDic mutableCopy] fileName:fileNameStr saveDoc:CTFileDocLibrary savePath:@"cacheData/homepage/subjectList"];
                    
                }
                else{
                    
                    
                    
                    NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                    [CTDebugManager showErrorLogInfo:errDescription];
                }
            }
        }
        else
        {
            [CTDebugManager showErrorLogInfo:@"解析首页热点数据错误！"];
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"解析首页热点数据错误！"];
    }
    
    return isParserSuccess;
}





@end
